using System;
using System.Collections.Generic;
using OS.Infrastructure.Domain;

namespace TaskKernel.Domain
{
    public class Account : AbstractEntity
    {
        protected ICollection<Account> masterAccounts;
        protected ICollection<Account> slaveAccounts;

        public Account()
        {
            masterAccounts = new HashSet<Account>();
            slaveAccounts = new HashSet<Account>();
        }

        public Account(string login)
            : this()
        {
            Login = login;
            UID = Guid.NewGuid();
        }

        public virtual string Login { get; set; }
        public virtual string Phone { get; set; }
        public virtual string Mail { get; set; }
        public virtual string SID { get; set; }
        public virtual string FullName { get; set; }
        public virtual string DepartmentName { get; set; }
        public virtual string JobTitle { get; set; }
        public virtual bool Fired { get; set; }

        public virtual IEnumerable<Account> MasterAccounts
        {
            get { return masterAccounts; }
            protected set { }
        }

        public virtual IEnumerable<Account> SlaveAccounts
        {
            get { return slaveAccounts; }
            protected set { }
        }

        public virtual Account SetFullName(string fullName)
        {
            FullName = fullName;
            return this;
        }

        public virtual void SetMail(string eMailAddress)
        {
            Mail = eMailAddress;
        }

        public virtual bool AddManager(Account account)
        {
            if (!masterAccounts.Contains(account))
            {
                masterAccounts.Add(account);
                return true;
            }
            return false;
        }

        public virtual bool RemoveManager(Account account)
        {
            return masterAccounts.Remove(account);
        }

        public virtual bool AddSlaveUser(Account account)
        {
            if (!slaveAccounts.Contains(account))
            {
                slaveAccounts.Add(account);
                return true;
            }
            return false;
        }

        public virtual bool RemoveSlaveUser(Account account)
        {
            return slaveAccounts.Remove(account);
        }

        public override int GetHashCode()
        {
            int hashCode = 0;
            hashCode = hashCode ^ UID.GetHashCode();
            return hashCode;
        }

        public override bool Equals(object obj)
        {
            var toCompare = obj as Account;
            if (toCompare == null)
                return false;
            return (GetHashCode() == toCompare.GetHashCode());
        }
    }
}